package com.ztjy.sys.biz.impl;

import com.ztjy.bean.Module;
import com.ztjy.bean.Permission;
import com.ztjy.sys.biz.PermissionBiz;
import com.ztjy.sys.dao.PermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionBizImpl implements PermissionBiz {
    @Autowired
    private PermissionDao pdao;

    @Override
    public List<Module> findMoudleByJob(Integer jobId, Integer pstate, Integer flag) {
        List<Module> modules = pdao.findMoudleByJob(jobId, pstate, flag);
        return modules;
    }

    @Override
    public Boolean grantPermission(Integer jobId, List<Permission> plist) {
        boolean flag = false;
        //先根据职位id获取 其原有的主键id
        List<Integer> pids = pdao.findPids(jobId);
        try {
            //先删除
            if (pids.size() > 0) {
                flag = pdao.deletePermissionAllById(pids);
            } else {
                flag = true;
            }
            if (flag) {//若删除成功 继续下一步
                flag = pdao.inserPermissionList(plist);
            }
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deletePermissionAllById(List<Integer> ids) {
        boolean flag = pdao.deletePermissionAllById(ids);
        return flag;
    }
}
